package model;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;


public class Plant {
	private int id;
	private int typeID;
	private String name;
	private int optCO2;
	private int optTemp;
	private int optFugt;
	private int optLigt;
	private int price;
	private double growthDays;
	private double remainingGrowthDays;
	private double prodState;
	private double photoSyntese;
	private ArrayList<Double> plantGrowthHisto;
	private String pictureLocation;
	private JButton tile;
 ModelClass modelClass;
	private Variables variables;
	private String[] nameStringArray;

	public Plant(ModelClass modelClass, int typeID, String name, int optCO2,
			int optTemp, int optFugt, int optLigt, int price,
			String pictureLocation) {
		this.modelClass = modelClass;
		this.variables = modelClass.getVariables();
		this.typeID = typeID;
		this.name = name;
		this.optCO2 = optCO2;
		this.optTemp = optTemp;
		this.optFugt = optFugt;
		this.optLigt = optLigt;
		this.price = price;
		this.prodState = prodState;
		this.modelClass = modelClass;
		this.pictureLocation = pictureLocation;
		this.growthDays = 100;
		this.photoSyntese = 0;
		this.remainingGrowthDays = growthDays;
		plantGrowthHisto = new ArrayList<Double>();
	}

	// Creates a tile with the plant image
	public JButton getPlantTile() {

		tile = new JButton();
		tile = new JButton();
		tile.setIcon(new ImageIcon(process(variables.getImgPlantTileIcon())));
		tile.setBackground(new Color(34, 177, 76));
		tile.setBorder(null);
		return tile;
	}

	public BufferedImage process(BufferedImage old) {
		int w = old.getWidth();
		int h = old.getHeight();
		BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2d = img.createGraphics();
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		Font font = variables.getFontTileMain();
		font = font.deriveFont(15f);
		g2d.setFont(font);
		g2d.drawImage(old, 0, 0, null);

		if (name.length() >= 15) {
			nameStringArray = name.split("\\s+");
			g2d.drawString(nameStringArray[0], 15, 113);
			g2d.drawString(nameStringArray[1], 15, 128);
		} else {

			g2d.drawString(name, 15, 120);
		}
		return img;
	}

	// Method that calculates the production-sate and updates it.
	public void updateProdstate(int curTemp, int curLight, int curCO2,
			int curHumd) {
		double prTemp = Math.abs(((optTemp / (double) curTemp - 1) * 100));
		double prLight = Math
				.abs((((double) optLigt / (double) curLight - 1) * 100));
		double prCO2 = Math
				.abs((((double) optCO2 / (double) curCO2 - 1) * 100));
		double prHumidity = Math
				.abs((((double) optFugt / (double) curHumd - 1) * 100));

		photoSyntese = 1 - (prTemp + prLight + prCO2 + prHumidity) / 100;
		if (photoSyntese < 0) {
			photoSyntese = 0;
		}
		remainingGrowthDays = remainingGrowthDays - (photoSyntese);
		prodState = growthDays - remainingGrowthDays;
		plantGrowthHisto.add(photoSyntese);
		plantGrowthHisto.add(prodState);

	}

	// Getter and setters
	public ArrayList<Double> getPlantGrowthHisto() {
		return plantGrowthHisto;
	}

	public void setPlantGrowthHisto(ArrayList<Double> plantGrowthHisto) {
		this.plantGrowthHisto = plantGrowthHisto;
	}

	public int getTypeID() {
		return typeID;
	}

	public void setTypeID(int typeID) {
		this.typeID = typeID;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getOptCO2() {
		return optCO2;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public double getPhotoSyntese() {
		return photoSyntese;
	}

	public void setPhotoSyntese(double photoSyntese) {
		this.photoSyntese = photoSyntese;
	}

	public void setOptCO2(int optCO2) {
		this.optCO2 = optCO2;
	}

	public int getOptTemp() {
		return optTemp;
	}

	public void setOptTemp(int optTemp) {
		this.optTemp = optTemp;
	}

	public int getOptFugt() {
		return optFugt;
	}

	public void setOptFugt(int optFugt) {
		this.optFugt = optFugt;
	}

	public int getOptLigt() {
		return optLigt;
	}

	public void setOptLigt(int optLigt) {
		this.optLigt = optLigt;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public double getProdState() {
		return prodState;
	}

	public void setProdState(int prodState) {
		this.prodState = prodState;
	}

	public String getPictureLocation() {
		return pictureLocation;
	}

	public void setPictureLocation(String pictureLocation) {
		this.pictureLocation = pictureLocation;
	}

}
